/** 
  校际交流项目导师审核、学院审核
*/
<template>
    <div>
        <div v-if="ifShowList === 'list'">
            <condition-card
                @reset="reset"
                @search="onSubmit"
                label-position="right"
                label-width="150px"
            >
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item label="学院">
                            <el-select
                                v-model.trim="queryForm.condition.faculty"
                                clearable
                                style="width: 100%"
                                filterable
                                @change="changeCollege"
                                placeholder
                                :disabled="!ifAdmin"
                            >
                                <el-option
                                    v-for="item in options.faculty"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="专业">
                            <el-select
                                v-model.trim="queryForm.condition.profession"
                                clearable
                                filterable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.profession"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目负责人姓名">
                            <el-input
                                v-model="queryForm.condition.principal"
                                class="topic-input-select"
                                maxlength="32"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="项目开始时间">
                            <el-date-picker
                                v-model="queryForm.condition.startTime"
                                type="date"
                                clearable
                                style="width: 100%"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="startDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目结束时间">
                            <el-date-picker
                                v-model="queryForm.condition.endTime"
                                type="date"
                                clearable
                                style="width: 100%"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="endDatePicker"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目级别">
                            <el-select
                                v-model.trim="queryForm.condition.projectLevel"
                                clearable
                                style="width: 100%"
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.projectLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="8">
                        <el-form-item label="学号">
                            <el-input
                                v-model="queryForm.condition.studentId"
                                class="topic-input-select"
                                maxlength="32"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="姓名">
                            <el-input
                                style="width: 100%"
                                v-model="queryForm.condition.studentName"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核状态">
                            <el-select
                                v-model.trim="queryForm.condition.approveStatus"
                                clearable
                                style="width: 100%"
                                filterable
                                placeholder
                            >
                                <el-option
                                    v-for="item in options.states"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- 搜索框结束 -->

                <!-- 列表页开始 -->
            </condition-card>
            <el-card shadow="never" v-loading="loading" body-style="padding: 24px;">
                <h1 class="condition-title">校际交流项目学生申请审核列表</h1>
                <div class="sub-title">
                    <div class="float-right" style="display: flex;">
                        <!-- 导出 -->
                        <el-button @click="exportMenu()">导出</el-button>
                        <!-- 驳回 -->
                        <el-button  @click="auditStu(2)" v-show="ifReturn">
                            驳回
                        </el-button>
                        <!-- 不通过 -->
                        <el-button  @click="auditStu(1)" v-show="ifRefuse">
                            不通过
                        </el-button>
                        <!-- 通过 -->
                        <el-button type="primary" @click="auditStu(0)" v-show="ifPass">
                            通过
                        </el-button>
                    </div>
                </div>
                <edu-table
                    :pagination="false"
                    :order="true"
                    :tooltipEffect="tooltipEffect"
                    stripe
                    :data="dataList"
                    size="mini"
                    @selection-change="handleSelectionChange"
                >
                    <edu-table-column label="学号" prop="studentId"></edu-table-column>
                    <edu-table-column label="姓名" prop="studentName"></edu-table-column>
                    <edu-table-column label="专业" prop="professionName"></edu-table-column>
                    <edu-table-column label="项目名称" prop="projectName"></edu-table-column>
                    <edu-table-column
                        label="项目所涉及的学院"
                        prop="involveCollege"
                        width="160"
                    ></edu-table-column>
                    <edu-table-column label="国家/地区" prop="nation"></edu-table-column>
                    <edu-table-column label="合作大学" prop="partnerCollege"></edu-table-column>
                    <edu-table-column
                        label="项目开始时间"
                        prop="startTime"
                        width="120"
                    ></edu-table-column>
                    <edu-table-column
                        label="项目结束时间"
                        prop="endTime"
                        width="120"
                    ></edu-table-column>
                    <edu-table-column label="项目级别" prop="projectLevel">
                        <template slot-scope="scope">
                            <el-tag>{{ scope.row.projectLevel | projectLevel }}</el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column label="审核状态" prop="approveStatus">
                        <template slot-scope="scope">
                            <el-tag
                                v-if="scope.row.approveStatus !== null"
                                :type="scope.row.approveStatus | getType"
                            >
                                {{ scope.row.approveStatus | approveStatus }}
                            </el-tag>
                        </template>
                    </edu-table-column>
                    <edu-table-column label="下一级审核人" prop="approveName"></edu-table-column>
                    <!-- 操作 -->
                    <edu-table-column :label="$t('base.operate')" fit width="140">
                        <template slot-scope="scope">
                            <el-button type="text" @click="viewAudit(scope.row)">查看</el-button>
                            <el-button
                                type="text"
                                v-show="scope.row.canApprove"
                                @click="audit(scope.row)"
                            >
                                审核
                            </el-button>
                        </template>
                    </edu-table-column>
                </edu-table>
                <el-row type="flex" class="row-bg" justify="end">
                    <el-col>
                        <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="queryForm.pageNum_"
                            :page-sizes="[20, 40, 80, 100]"
                            :page-size="queryForm.pageSize_"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="queryForm.total_"
                            style="float:right;margin-top:10px"
                        ></el-pagination>
                    </el-col>
                </el-row>
            </el-card>
        </div>
        <div v-if="ifShowList === 'view'">
            <stu-Apply-View
                @returnVal="changekey"
                :ifAudit="ifAudit"
                :id="id"
                :actNo="actNo"
                :studentId="studentId"
                :pageFrom="pageFrom"
            ></stu-Apply-View>
        </div>
        <el-dialog
            title="审核意见"
            :visible.sync="dialogFormVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-form ref="contentForm" :model="contentForm">
                <el-form-item label="审批说明" prop="content">
                    <el-input
                        type="textarea"
                        :rows="4"
                        placeholder="请输入内容"
                        maxlength="500"
                        v-model="contentForm.content"
                    ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeContent">取 消</el-button>
                <el-button type="primary" @click="auditSubmit">确 定</el-button>
            </div>
        </el-dialog>
        <edu-export-dialog :title="$t('message.prompt')" ref="downloadRef"></edu-export-dialog>
    </div>
</template>
<script>
import conOfIntAcaConApi from 'common/src/api/internationalCommunication/conOfIntAcaCon'
import stuApplyApi from 'common/src/api/interSchoolExchange/stuApply'
import { queryDic } from 'common/src/api/dictionary'
import baseUrl from 'common/src/api/interSchoolExchange/env'
import UTILS from 'common/src/utils/utils'
import stuApplyView from './common/stuApplyView'
export default {
    components: {
        'stu-Apply-View': stuApplyView
    },
    data() {
        return {
            startDatePicker: this.beginDate(),
            endDatePicker: this.processDate(),
            studentId: '',
            pageFrom: '',
            queryForm: {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    schoolApprove: false,
                    projectLevel: '',
                    faculty: '',
                    profession: '',
                    principal: '',
                    approveStatus: '',
                    startTime: null,
                    endTime: null,
                    studentId: '',
                    studentName: ''
                }
            },
            dataList: [],
            contentForm: {
                content: ''
            },
            auditType: '',
            ifShowList: 'list',
            ifAudit: false,
            actNo: '',
            id: null,
            auths: [],
            ifPass: false,
            ifReturn: false,
            ifRefuse: false,
            ifAdmin: false,
            dialogFormVisible: false,
            multipleSelection: {
                ids: [],
                actNo: [],
                status: []
            },
            options: {
                faculty: [],
                profession: [],
                states: [
                    {
                        label: '已提交',
                        value: 1
                    },
                    {
                        label: '待审核',
                        value: 2
                    },

                    {
                        label: '不通过',
                        value: 4
                    },
                    {
                        label: '已通过',
                        value: 5
                    }
                ],
                projectLevel: [
                    {
                        value: 1,
                        label: '院级'
                    },
                    {
                        value: 2,
                        label: '校级'
                    }
                ]
            }
        }
    },
    created() {
        this.initOptions()
        if (localStorage.getItem('sessiondata')) {
            let flag = false
            // 获取当前用户auths权限
            if (JSON.parse(localStorage.getItem('sessiondata')).auths) {
                this.auths = JSON.parse(localStorage.getItem('sessiondata')).auths
                if (this.auths) {
                    this.auths.forEach(val => {
                        if (val.authCode === 'interSchool-tempSet-ifAdmin') {
                            flag = true
                        }
                    })
                }
            }
            // 获取登陆用户学院
            if (JSON.parse(localStorage.getItem('sessiondata')).mockUser) {
                this.queryFaculty = JSON.parse(localStorage.getItem('sessiondata')).mockUser.faculty
                if (flag) {
                    this.queryForm.condition.faculty = ''
                } else {
                    this.queryForm.condition.faculty = this.queryFaculty
                }

                this.changeCollege(this.queryForm.condition.faculty)
            } else if (JSON.parse(localStorage.getItem('sessiondata')).user) {
                this.queryFaculty = JSON.parse(localStorage.getItem('sessiondata')).user.faculty
                if (flag) {
                    this.queryForm.condition.faculty = ''
                } else {
                    this.queryForm.condition.faculty = this.queryFaculty
                }
                this.changeCollege(this.queryForm.condition.faculty)
            }
        }
    },
    mounted() {
        // 初始化列表页
        this.getListData()
         
    },
    filters: {
        approveStatus(val) {
            switch (val) {
                case 0:
                    return '草稿'
                case 1:
                    return '已提交'
                case 2:
                    return '待审核'
                case 3:
                    return '已驳回'
                case 4:
                    return '不通过'
                case 5:
                    return '已通过'

                default:
                    return ' '
            }
        },
        getType(val) {
            switch (val) {
                case 0:
                    return 'info'
                case 1:
                    return 'info'
                case 2:
                    return 'info'
                case 3:
                    return 'warning'
                case 4:
                    return 'danger'
                case 5:
                    return 'success'
                default:
                    return ''
            }
        },
        projectLevel(val) {
            switch (val) {
                case 1:
                    return '院级'
                case 2:
                    return '校级'
                default:
                    return ''
            }
        }
    },
    watch: {
        auths: {
            handler(newVal) {
                if (newVal && Array.isArray(newVal) && newVal.length > 0) {
                    newVal.some(item => {
                        // 批量通过
                        if (item.authCode === 'interSchool-tempSet-ifPass') {
                            this.ifPass = true
                        }
                        // 批量拒绝
                        if (item.authCode === 'interSchool-tempSet-ifRefuse') {
                            this.ifRefuse = true
                        }
                        // 批量驳回
                        if (item.authCode === 'interSchool-tempSet-ifReturn') {
                            this.ifReturn = true
                        }
                        // 判断校级管理员
                        if (item.authCode === 'interSchool-tempSet-ifAdmin') {
                            this.ifAdmin = true
                            this.queryForm.condition.schoolApprove = true
                        }
                    })
                }
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        async initOptions() {
            // 筛选条件字典查询
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: [
                    'X_YX' // 学院
                ]
            }
            await queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        faculty: 'X_YX' // 学院
                    },
                    res.data,
                    this.options
                )
            })
        },
        // 修改学院
        changeCollege(val) {
            this.options.profession = []
            this.queryForm.condition.profession = ''
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['G_ZY'],
                filter: {
                    prefix: [val],
                    specilaCode: 'XY2ZY',
                    facultys: [val]
                }
            }
            queryDic(data).then(res => {
                UTILS.fillSelect(
                    {
                        profession: 'G_ZY' // 专业
                    },
                    res.data,
                    this.options
                )
            })
        },

        beginDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.endTime) {
                        //如果结束时间不为空，则小于结束时间
                        return (
                            new Date(_this.queryForm.condition.endTime).getTime() < time.getTime()
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },
        processDate() {
            let _this = this
            return {
                disabledDate(time) {
                    if (_this.queryForm.condition.startTime) {
                        //如果开始时间不为空，则结束时间大于开始时间
                        return (
                            new Date(_this.queryForm.condition.startTime).getTime() >
                            time.getTime() + 24 * 60 * 60 * 1000
                        )
                    } else {
                        // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
                    }
                }
            }
        },
        // 导出
        exportMenu() {
            let _this = this
            let parm = {
                faculty: this.queryForm.condition.faculty,
                schoolApprove: this.ifAdmin,
                projectLevel: this.queryForm.condition.projectLevel,
                profession: this.queryForm.condition.profession,
                principal: this.queryForm.condition.principal,
                approveStatus: this.queryForm.condition.approveStatus,
                startTime: this.queryForm.condition.startTime,
                endTime: this.queryForm.condition.endTime,
                studentId: this.queryForm.condition.studentId,
                studentName: this.queryForm.condition.studentName,
                ids: this.multipleSelection.ids
            }
            if(!this.ifAdmin) {
            parm.faculty = ''
            }
            if (this.dataList.length === 0) {
                this.$message.error('无数据')
            } else {
                this.$refs.downloadRef.open()
                stuApplyApi.exportApprove(parm).then(res => {
                    if (res.data) {
                        let path = `${baseUrl}/sysDictionary/downFile?path=` + res.data
                        window.open(path)
                        this.$message({
                            message: this.$t('base.exportSuccess'),
                            type: 'success'
                        })
                        this.$refs.downloadRef.close()
                    }
                })
            }
        },

        // 申请页返回
        changekey(val) {
            this.ifShowList = val
            this.getListData()
        },
        // 关闭弹框
        closeContent() {
            this.dialogFormVisible = false
        },

        // 查看
        viewAudit(val) {
            this.ifShowList = 'view'
            this.ifAudit = false
            this.id = val.id
            this.actNo = val.actNo
            this.studentId = val.studentId
            this.pageFrom = 'auditView'
        },
        // 审核
        audit(val) {
            this.ifShowList = 'view'
            this.ifAudit = true
            this.id = val.id
            this.actNo = val.actNo
            this.studentId = val.studentId
            this.pageFrom = 'audit'
        },
        // 打开审核窗口
        auditStu(type) {
            let flag = true
            if (this.multipleSelection.actNo.length === 0) {
                this.$message.error('请勾选数据')
            } else {
                this.multipleSelection.status.forEach(val => {
                    if (val.canApprove !== true) {
                        flag = false
                    }
                })

                if (flag) {
                    this.contentForm.content = ''
                    this.auditType = type
                    this.dialogFormVisible = true
                } else {
                    this.$message.error('当前勾选存在不可审核数据')
                }
            }
        },

        auditSubmit() {
            let parm = {
                approveType: this.auditType,
                remark: this.contentForm.content,
                actNo: this.multipleSelection.actNo
            }
            if (this.auditType === 2 && this.contentForm.content === '') {
                this.$message.error('审批说明不可为空')
            } else {
                if (this.multipleSelection.actNo.length > 0) {
                    stuApplyApi.approveApply(parm).then(res => {
                        if (res.code === 200) {
                            this.$message({
                                message: '审核成功',
                                type: 'success'
                            })
                            this.dialogFormVisible = false
                            this.getListData()
                        }
                    })
                }
            }
        },

        // 重置筛选框
        reset() {
            this.queryForm = {
                pageNum_: 1,
                pageSize_: 20,
                total_: 0,
                condition: {
                    schoolApprove: this.ifAdmin,
                    projectLevel: '',
                    faculty: this.queryFaculty,
                    profession: '',
                    principal: '',
                    approveStatus: '',
                    startTime: '',
                    endTime: '',
                    studentId: '',
                    studentName: ''
                }
            }
            this.getListData()
        },

        // 查询按钮
        onSubmit() {
            this.queryForm.pageNum_ = 1
            this.queryForm.total_ = 0
            this.queryForm.pageSize_ = 20
            this.getListData()
        },

        // 获取列表
        getListData() {
            let _this = this
            this.dataList = []
            this.multipleSelection.ids = []
            this.multipleSelection.actNo = []
            this.multipleSelection.status = []
            this.queryForm.condition.studentId = this.queryForm.condition.studentId.trim()
            this.queryForm.condition.studentName = this.queryForm.condition.studentName.trim()
            this.queryForm.condition.principal = this.queryForm.condition.principal.trim()
            let parm = {}
             parm = _.cloneDeep(this.queryForm)
            if(!this.ifAdmin) {
            parm.condition.faculty = ''
            }
            stuApplyApi.listApprove(parm).then(res => {
                res.data.list.forEach(revList => {
                    _this.dataList.push(revList)
                    _this.queryForm.pageSize_ = res.data.pageSize_
                    _this.queryForm.total_ = res.data.total_
                    _this.queryForm.pageNum_ = res.data.pageNum_
                })
            })
        },
        handleSelectionChange(val) {
            this.multipleSelection.ids = []
            this.multipleSelection.actNo = []
            this.multipleSelection.status = []
            val.forEach(menu => {
                this.multipleSelection.ids.push(menu.id)
                this.multipleSelection.actNo.push(menu.actNo)
                this.multipleSelection.status.push(menu)
            })
        },
        handleSizeChange(val) {
            this.queryForm.pageSize_ = val
            this.getListData()
        },
        handleCurrentChange(val) {
            this.queryForm.pageNum_ = val
            this.getListData()
        }
    }
}
</script>
<style lang="scss" scoped>
.condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
}
.sub-title {
    margin: 16px 0 65px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
}
</style>
